using System.Collections.Generic;
using UnityEngine;

public class ResultUIDTB : MonoBehaviour
{
	private GameObject pauseobj;

	public UIEventListener mainbtn;

	private List<planefightinfo> enemyfightinfo;

	private List<planefightinfo> frifightinfo;

	public List<UIfightinfo> infoLabelsFri;

	public List<UIfightinfo> infoLabelsAI;

	public UILabel friScore;

	public UILabel aiScore;

	public GameObject titlename;

	public UILabel falsename;

	public UISlider expslider;

	public UISprite currankflag;

	public UISprite nextrankflag;

	public UILabel exp_label;

	private float curexp;

	private float rateexp;

	private int endexp;

	private bool exprunflag;

	public ChallengeData challdata;

	public UILabel explabel;

	public GameObject[] rewardobj;

	public UILabel moneylabel;

	public UILabel diamondlabel;

	public GameObject diamondobj;

	public GameObject pieceobj;

	public Piece_item pieceitem;

	private bool iswin;

	private int ranknum;

	public UIWidget colliderwi;

	private bool poprankui;

	private int firstlevel;

	private bool temp_first;

	private void Start()
	{
		mainbtn.onClick = OnClickmainBtn;
	}

	private void Update()
	{
		if (exprunflag)
		{
			if (curexp < (float)endexp)
			{
				curexp += Time.deltaTime * rateexp;
			}
			else
			{
				curexp = endexp;
				exprunflag = false;
			}
			expshow((int)curexp);
		}
	}

	public void init_ui_info()
	{
		enemyfightinfo = AIPool.instance.GetFightInfo();
		frifightinfo = FriendPool.instance.GetFightInfo();
		frifightinfo.Add(plane_smothfollow.instance.GetPlayerFightinfo());
		sortinfo();
		int num = 0;
		int num2 = 0;
		for (int i = 0; i < 5; i++)
		{
			if (i < enemyfightinfo.Count)
			{
				num += enemyfightinfo[i].selfinfo.killnum_build;
				infoLabelsAI[i].name.text = i + 1 + ". " + GlobalInf.ai_names[enemyfightinfo[i].planeindex];
				infoLabelsAI[i].kill.text = string.Empty + enemyfightinfo[i].selfinfo.killnum_plane;
				infoLabelsAI[i].death.text = string.Empty + enemyfightinfo[i].selfinfo.diednum;
				infoLabelsAI[i].build.text = string.Empty + enemyfightinfo[i].selfinfo.killnum_build;
			}
			else
			{
				infoLabelsAI[i].name.text = string.Empty;
				infoLabelsAI[i].kill.text = string.Empty;
				infoLabelsAI[i].death.text = string.Empty;
				infoLabelsAI[i].build.text = string.Empty;
			}
			if (i < frifightinfo.Count)
			{
				num2 += frifightinfo[i].selfinfo.killnum_build;
				if (frifightinfo[i].planeindex == -1)
				{
					ranknum = i;
					infoLabelsFri[i].name.text = i + 1 + ". You";
					infoLabelsFri[i].bg.spriteName = "JS_wenZiKuangC_TZ";
				}
				else
				{
					infoLabelsFri[i].name.text = i + 1 + ". " + GlobalInf.fri_names[frifightinfo[i].planeindex];
					if (i % 2 == 0)
					{
						infoLabelsFri[i].bg.spriteName = "JS_wenZiKuangB_TZ";
					}
					else
					{
						infoLabelsFri[i].bg.spriteName = "JS_wenZiKuangA_TZ";
					}
				}
				infoLabelsFri[i].kill.text = string.Empty + frifightinfo[i].selfinfo.killnum_plane;
				infoLabelsFri[i].death.text = string.Empty + frifightinfo[i].selfinfo.diednum;
				infoLabelsFri[i].build.text = string.Empty + frifightinfo[i].selfinfo.killnum_build;
			}
			else
			{
				infoLabelsFri[i].name.text = string.Empty;
				infoLabelsFri[i].kill.text = string.Empty;
				infoLabelsFri[i].death.text = string.Empty;
				infoLabelsFri[i].build.text = string.Empty;
				if (i % 2 == 0)
				{
					infoLabelsFri[i].bg.spriteName = "JS_wenZiKuangB_TZ";
				}
				else
				{
					infoLabelsFri[i].bg.spriteName = "JS_wenZiKuangA_TZ";
				}
			}
		}
		aiScore.text = "得分:" + num;
		friScore.text = "得分:" + num2;
		if (num <= num2 && num2 != 0)
		{
			falsename.enabled = false;
			NGUITools.SetActive(titlename, state: true);
			iswin = true;
			Platform.flurryEvent_challenge(3, GlobalInf.chanllenge_mode, 4);
			MonoInstance<AudioController>.instance.play(AudioType.UI_gameover_suc, 0f);
		}
		else
		{
			falsename.enabled = true;
			NGUITools.SetActive(titlename, state: false);
			iswin = false;
			Platform.flurryEvent_challenge(3, GlobalInf.chanllenge_mode, 5);
		}
		rewardShow();
		saveAchi();
	}

	public void saveAchi()
	{
		GamePlayerprefs.instance.readachievement();
		GamePlayerprefs.instance.readdailytask();
		GamePlayerprefs.instance.savefightinfo();
		GamePlayerprefs.instance.additemachi(10, 1);
		if (iswin)
		{
			GamePlayerprefs.instance.additemachi(16, 1);
		}
		GamePlayerprefs.instance.savedailytask(2);
	}

	public void rewardShow()
	{
		challdata = DataController.instance.dtbdata[GlobalInf.chanllenge_mode].challdata;
		float num = DataController.instance.resultratio[ranknum];
		float num2 = (300f - TasksController.instance.defendthebase.TimeCal) / 300f;
		int num3 = 0;
		int num4 = 0;
		int num5 = 0;
		float num6 = 300f - TasksController.instance.defendthebase.TimeCal;
		if (num6 < 60f)
		{
			Platform.flurryEvent_challenge(3, GlobalInf.chanllenge_mode, 1);
		}
		else if (num6 < 180f)
		{
			Platform.flurryEvent_challenge(3, GlobalInf.chanllenge_mode, 2);
		}
		else
		{
			Platform.flurryEvent_challenge(3, GlobalInf.chanllenge_mode, 3);
		}
		if (iswin)
		{
			num2 = 1f;
			num3 = (int)((float)challdata.exp * num * num2);
			num5 = (int)((float)challdata.money * num * num2);
			num4 = (int)((float)challdata.diamond * num * num2);
			if (num3 <= 0)
			{
				num3 = 1;
			}
			if (num5 <= 0)
			{
				num5 = 1;
			}
			if (num4 <= 0)
			{
				num4 = 1;
			}
			explabel.text = string.Empty + num3;
			moneylabel.text = string.Empty + num5;
			diamondlabel.text = string.Empty + num4;
			curexp = GamePlayerprefs.instance.allexp;
			endexp = (int)curexp + num3;
			rateexp = (float)num3 / 2f;
			expshow((int)curexp);
			exprunflag = true;
			if (challdata.dropitem.dropid != -1)
			{
				if (UnityEngine.Random.Range(1, 101) <= challdata.dropitem.gailv)
				{
					GamePlayerprefs.instance.readpiece();
					PieceData pieceData = DataController.instance.Getpiece(challdata.dropitem.dropid);
					pieceitem.enable(pieceData);
					GamePlayerprefs.instance.addpiece(pieceData.id, 1);
				}
				else
				{
					NGUITools.SetActive(pieceobj, state: false);
				}
			}
			else
			{
				NGUITools.SetActive(pieceobj, state: false);
			}
			GamePlayerprefs.instance.add_exp(num3);
			GamePlayerprefs.instance.add_money(num5);
			GamePlayerprefs.instance.add_diamond(num4);
		}
		else
		{
			num3 = (int)((float)challdata.falseexp * num * num2);
			num5 = (int)((float)challdata.falsemoney * num * num2);
			curexp = GamePlayerprefs.instance.allexp;
			endexp = (int)curexp + num3;
			rateexp = (float)num3 / 2f;
			expshow((int)curexp);
			exprunflag = true;
			if (num3 <= 0)
			{
				num3 = 1;
			}
			if (num5 <= 0)
			{
				num5 = 1;
			}
			explabel.text = string.Empty + num3;
			moneylabel.text = string.Empty + num5;
			NGUITools.SetActive(diamondobj, state: false);
			NGUITools.SetActive(pieceobj, state: false);
			GamePlayerprefs.instance.add_exp(num3);
			GamePlayerprefs.instance.add_money(num5);
		}
	}

	public void sortinfo()
	{
		int num2;
		for (int num = enemyfightinfo.Count - 1; num > 0; num = num2)
		{
			num2 = 0;
			for (int i = 0; i < num; i++)
			{
				if (enemyfightinfo[i].selfinfo.killnum_build < enemyfightinfo[i + 1].selfinfo.killnum_build)
				{
					num2 = i;
					planefightinfo value = enemyfightinfo[i];
					enemyfightinfo[i] = enemyfightinfo[i + 1];
					enemyfightinfo[i + 1] = value;
				}
				else if (enemyfightinfo[i].selfinfo.killnum_build == enemyfightinfo[i + 1].selfinfo.killnum_build)
				{
					if (enemyfightinfo[i].selfinfo.killnum_plane < enemyfightinfo[i + 1].selfinfo.killnum_plane)
					{
						num2 = i;
						planefightinfo value2 = enemyfightinfo[i];
						enemyfightinfo[i] = enemyfightinfo[i + 1];
						enemyfightinfo[i + 1] = value2;
					}
					else if (enemyfightinfo[i].selfinfo.killnum_plane == enemyfightinfo[i + 1].selfinfo.killnum_plane && enemyfightinfo[i].selfinfo.diednum > enemyfightinfo[i + 1].selfinfo.diednum)
					{
						num2 = i;
						planefightinfo value3 = enemyfightinfo[i];
						enemyfightinfo[i] = enemyfightinfo[i + 1];
						enemyfightinfo[i + 1] = value3;
					}
				}
			}
		}
		int num3;
		for (int num = frifightinfo.Count - 1; num > 0; num = num3)
		{
			num3 = 0;
			for (int j = 0; j < num; j++)
			{
				if (frifightinfo[j].selfinfo.killnum_build < frifightinfo[j + 1].selfinfo.killnum_build)
				{
					num3 = j;
					planefightinfo value4 = frifightinfo[j];
					frifightinfo[j] = frifightinfo[j + 1];
					frifightinfo[j + 1] = value4;
				}
				else if (frifightinfo[j].selfinfo.killnum_build == frifightinfo[j + 1].selfinfo.killnum_build)
				{
					if (frifightinfo[j].selfinfo.killnum_plane < frifightinfo[j + 1].selfinfo.killnum_plane)
					{
						num3 = j;
						planefightinfo value5 = frifightinfo[j];
						frifightinfo[j] = frifightinfo[j + 1];
						frifightinfo[j + 1] = value5;
					}
					else if (frifightinfo[j].selfinfo.killnum_plane == frifightinfo[j + 1].selfinfo.killnum_plane && frifightinfo[j].selfinfo.diednum > frifightinfo[j + 1].selfinfo.diednum)
					{
						num3 = j;
						planefightinfo value6 = frifightinfo[j];
						frifightinfo[j] = frifightinfo[j + 1];
						frifightinfo[j + 1] = value6;
					}
				}
			}
		}
	}

	public void OnClickmainBtn(GameObject btn)
	{
		Platform.HideFeatureView();
		MenuUIController.beginshow = BeginShow.main;
		Time.timeScale = 1f;
		UnityEngine.SceneManagement.SceneManager.LoadScene("Menu");
		MidLoading.loadlevel = 0;
	}

	public void expshow(int expnum)
	{
		expdata exp = GamePlayerprefs.instance.GetExp(expnum);
		if (exp.level >= 19)
		{
			expslider.value = 1f;
			exp_label.text = string.Empty;
			currankflag.spriteName = "19." + GamePlayerprefs.instance.ranknameImage[exp.level - 1];
			nextrankflag.enabled = false;
			currankflag.SetDimensions(56, 48);
			nextrankflag.SetDimensions(56, 48);
		}
		else
		{
			expslider.value = (float)exp.curexp / (float)exp.maxexp;
			exp_label.text = string.Empty + exp.curexp + "/" + exp.maxexp;
			currankflag.spriteName = string.Empty + exp.level + "." + GamePlayerprefs.instance.ranknameImage[exp.level - 1];
			nextrankflag.spriteName = string.Empty + (exp.level + 1) + "." + GamePlayerprefs.instance.ranknameImage[exp.level];
			if (exp.level >= 15)
			{
				currankflag.SetDimensions(56, 48);
				nextrankflag.SetDimensions(56, 48);
			}
			else if (exp.level < 14)
			{
				currankflag.SetDimensions(40, 44);
				nextrankflag.SetDimensions(40, 44);
			}
			if (exp.level == 14)
			{
				currankflag.SetDimensions(40, 44);
				nextrankflag.SetDimensions(56, 48);
			}
		}
		showrankeui(exp.level);
	}

	public void showrankeui(int level)
	{
		if (!poprankui)
		{
			if (!temp_first)
			{
				firstlevel = level;
				temp_first = true;
			}
			if (level == firstlevel + 1)
			{
				GameUIController.instance.enable_rankui();
				poprankui = true;
			}
		}
	}

	public void enable()
	{
		if (pauseobj == null)
		{
			pauseobj = base.gameObject;
		}
		NGUITools.SetActive(pauseobj, state: true);
		init_ui_info();
		colliderwi.alpha = 1f; colliderwi.gameObject.SetActive(true);
		Invoke("showad", 0.5f);
	}

	public void showad()
	{
		colliderwi.alpha = 0f; colliderwi.gameObject.SetActive(false);
		Platform.ShowFeatureView();
		Platform.ShowFullScreenSmall();
	}

	public void disable()
	{
		NGUITools.SetActive(pauseobj, state: false);
	}
}
